﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;

namespace System.Text.Json
{
    /// <summary>
    /// 针对异常对象<see cref="Exception"/>做序列化的转换处理
    /// </summary>
    /// <creator>marc</creator>
    public class ExceptionConverter : JsonConverter<Exception>
    {
        /// <inheritdoc/>
        public override Exception Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            throw new NotImplementedException("无法反序列回异常对象，只支持处理异常的序列化");
        }

        /// <inheritdoc/>
        public override void Write(Utf8JsonWriter writer, Exception value, JsonSerializerOptions options)
        {
            writer.WriteStartObject();
            writer.WriteString("Type", value.GetType().FullName);
            writer.WriteString("Message", value.Message);
            writer.WriteString("StackTrace", value.StackTrace);
            writer.WriteEndObject();
        }
    }
}
